package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.bean.OrderSellListBean;
import com.sskj.cndragon.presenter.SellOutSplitActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.mvchelper.NormalRefreshDataSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;


public class SellOutSplitActivity extends BaseActivity<SellOutSplitActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private String pollID;

    private SlimAdapter<OrderSellListBean.OrderSellBean> slimAdapter;
    private MVCCoolHelper<List<OrderSellListBean.OrderSellBean>> mvcHelper;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sell_out_split;
    }


    @Override
    protected void initView() {
        super.initView();
        pollID = getIntent().getStringExtra(Constans.POLL_ID);
        toolbar.setLeftButtonOnClickListener(view -> finish());

        recyclerView.setLayoutManager(new LinearLayoutManager(App.INSTANCE));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(App.INSTANCE)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_buy_in, new SlimInjector<OrderSellListBean.OrderSellBean>() {
            @Override
            public void onInject(OrderSellListBean.OrderSellBean data, IViewInjector injector, List payloads) {
                injector.clicked(R.id.constraint, v -> {
                    if (data.getSellStatus() == 2 || data.getSellStatus() == 3) {
                        OrderSellDetailActivity.start(App.INSTANCE, pollID, data.getOrderNo());
                    }
                })
                        .text(R.id.tv_status, data.getSellStatus() == 1 ? App.INSTANCE.getString(R.string.appWeimaichu) :
                                data.getSellStatus() == 2 ? App.INSTANCE.getString(R.string.appMaichuzhong) :
                                        data.getSellStatus() == 3 ? App.INSTANCE.getString(R.string.appYimaichu) :
                                                data.getSellStatus() == 4 ? App.INSTANCE.getString(R.string.appYushou) : "")
                        .text(R.id.tv_order, data.getOrderNo())
                        .text(R.id.tv_type, data.getName())
                        .text(R.id.tv_price, NumberUtil.keep2(data.getPrice()))
                        .text(R.id.tv_days, data.getContractDays())
                        .text(R.id.tv_incomes, NumberUtil.keepNoZero(data.getProfitRatio()) + "%")
                        .text(R.id.tv_date, TimeFormatUtil.formatE(data.getCreateTime()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        NormalRefreshDataSource<List<OrderSellListBean.OrderSellBean>> normalRefreshDataSource = new NormalRefreshDataSource<List<OrderSellListBean.OrderSellBean>>(coolRefreshView) {
            @Override
            public Flowable<List<OrderSellListBean.OrderSellBean>> loadData() {
                return mPresenter.getOrderSellSplitRecord(pollID);
            }
        };
        mvcHelper.setDataSource(normalRefreshDataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    public static void start(Context context, String orderID) {
        Intent intent = new Intent(context, SellOutSplitActivity.class);
        intent.putExtra(Constans.POLL_ID, orderID);
        context.startActivity(intent);
    }


    @Override
    public SellOutSplitActivityPresenter getPresenter() {
        return new SellOutSplitActivityPresenter();
    }
}
